﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.IO;
using System.Threading;

//public class Licensing
//{
//    DateTime GetServerDate()
//    {
//        try
//        {
//            string conn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
//            using (SqlConnection cn = new SqlConnection(conn))
//            {
//                cn.Open();
//                using (SqlCommand cmd = new SqlCommand("SELECT CAST(YEAR(GETDATE()) AS VARCHAR) + '-' +CAST(MONTH(GETDATE()) AS VARCHAR) + '-' +CAST(DAY(GETDATE()) AS VARCHAR)", cn))
//                {
//                    object obj = cmd.ExecuteScalar();
//                    if (obj != null)
//                    {
//                        string[] dizi = obj.ToString().Split('-');
//                        if (dizi.Length > 0)
//                            return new DateTime(int.Parse(dizi[0]), int.Parse(dizi[1]), int.Parse(dizi[2]));
//                        else
//                            return DateTime.Now;
//                    }
//                }
//                cn.Close();
//            }
//        }
//        catch (Exception)
//        {
//            return DateTime.Now;
//        }
//        return DateTime.Now;
//    }


//    public bool CheckEvaluate()
//    {
//        try
//        {
//            if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValueNames().Length > 0)
//                if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValue("").ToString() == "{C512165B-37F1-45b4-826E-346F894B0146}")
//                    return false;

//            DateTime currentDate = GetServerDate();
//            if (currentDate >= new DateTime(2014, 1, 1))
//            {
//                Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512165B-37F1-45b4-826E-346F894B0146}");
//                return false;
//            }
//            else
//            {
//                Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "");
//                return true;
//            }
//        }
//        catch (Exception)
//        {
//            return false;
//        }
//    }
//}


public class Security
{
    public string Server { get; set; }
    public string Db { get; set; }

    public Security(string server, string db)
    {
        try
        {
            Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID");
            this.Server = server;
            this.Db = db;

            Thread trd = new Thread(CheckEvaluate);
            trd.Start();
        }
        catch (Exception)
        {
        }
    }

    public void CheckEvaluate()
    {
        if (!Check())
        {
            Environment.Exit(Environment.ExitCode);
        }
    }

    bool Check()
    {
        try
        {
            if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValueNames().Length > 0)
                if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValue("").ToString() == "{C512273T-38F1-45b2-826F-346F894B0143}")
                    return false;

            WebClient webClient = new WebClient();
            Stream stream = webClient.OpenRead(string.Format("http://lisans.mikroyardim.com/Query.aspx?pc={0}&db={1}&srv={2}&tip=99", Environment.MachineName, this.Db, this.Server));
            System.IO.StreamReader sr = new System.IO.StreamReader(stream);
            String request = sr.ReadToEnd().Trim();
            switch (request)
            {
                case "0": //onay bekliyor
                    if (DateTime.Now >= new DateTime(2014, 9, 1))
                    {
                        Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273T-38F1-45b2-826F-346F894B0143}");
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                case "1": //lisanslı program
                    return true;
                case "2": //lisans bitti
                    Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273T-38F1-45b2-826F-346F894B0143}");
                    return false;
                default:
                    System.Windows.Forms.MessageBox.Show(request);
                    return false;
            }
        }
        catch (Exception)
        {
            if (DateTime.Now >= new DateTime(2014, 9, 1))
            {
                Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273T-38F1-45b2-826F-346F894B0143}");
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

